#pragma once

#include "utils/namespace_match.h"
#include "serializer/flatbuffer_serializer.h"
#include "serializer/protobuf_serializer.h"

#include <vector>

template<typename T>
class MsgSerializer {
public:
    static std::vector<uint8_t> serialize(const T& data) {
        if constexpr (is_fb_type<T>()) {
            return FlatBufferSerializer::serialize<T>(data);
        } else if constexpr (is_pb_type<T>()) {
            return ProtobufSerializer::serialize(data);
        } else {
            return std::vector<uint8_t>();
        }
    }

    template<typename DataType, typename IdlType>
    static DataType deserialize(const std::vector<uint8_t>& serialized_data) {
        DataType result;
        if constexpr (is_fb_type<DataType>()) {
            if(!FlatBufferSerializer::deserialize<DataType, IdlType>(serialized_data, result)) {
                throw std::runtime_error("Failed to deserialize FlatBuffer data");
            }
        } else if constexpr (is_pb_type<DataType>()) {
            if(!ProtobufSerializer::deserialize(serialized_data, result)) {
                throw std::runtime_error("Failed to deserialize Protobuf data");
            }
        }
        return result;
    }
};